package mq.util;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @description:
 * @author: zk
 * @since: 2022-08-17 10:58
 */
@Component
public class MqUtil {
    @Resource
    private MessageConverter messageConverter;

    /**
     * 将对象封装成Message对象,并开启消息持久化
     *
     * @param messageId
     * @param obj
     * @return
     */
    public Message getMessage(String messageId,Object obj) {
        // 封装Message
        MessageProperties properties = new MessageProperties();
        // 开启消息持久化
        properties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        // 设置MessageId
        properties.setMessageId(messageId);
        return messageConverter.toMessage(obj, properties);
    }

}
